/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "MusicTime.h"

MusicTime::MusicTime() throw(): m_time(0) {
}

MusicTime::MusicTime(unsigned long long ms) throw(): m_time(ms * 96) {
}

MusicTime::MusicTime(unsigned long long sample, unsigned int freq) throw(): m_time((96000 * sample) / freq) {
}

MusicTime::MusicTime(unsigned int beat, unsigned int bpm, unsigned int sigBpm, unsigned int sigBnt) throw(): m_time(beat * ((60 * 96000) / bpm)) {
}

MusicTime MusicTime::quantisize(unsigned int q, unsigned int bpm, unsigned int sigBpm, unsigned int sigBnt) const throw() {
    MusicTime qTime;
    MusicTime beatTime(1, bpm, sigBpm, sigBnt);
    unsigned long long time = m_time % beatTime.m_time;
    if(time < (beatTime.m_time / 2)) {
        // Quantisize down.
        qTime.m_time = m_time - time;
    }
    else {
        // Quantisize up.
        qTime.m_time = m_time + (beatTime.m_time - time);
    }
    return(qTime);
}

MusicTime MusicTime::operator +(const MusicTime &rhs) const throw() {
    MusicTime time;
    time.m_time = m_time + rhs.m_time;
    return(time);
}

MusicTime &MusicTime::operator +=(const MusicTime &rhs) throw() {
    m_time += rhs.m_time;
    return(*this);
}

MusicTime MusicTime::operator -(const MusicTime &rhs) const throw() {
    MusicTime time;
    time.m_time = m_time - rhs.m_time;
    return(time);
}

MusicTime &MusicTime::operator -=(const MusicTime &rhs) throw() {
    m_time -= rhs.m_time;
    return(*this);
}

bool MusicTime::operator ==(const MusicTime &rhs) const throw() {
    return(m_time == rhs.m_time);
}

bool MusicTime::operator !=(const MusicTime &rhs) const throw() {
    return(m_time != rhs.m_time);
}

bool MusicTime::operator <(const MusicTime &rhs) const throw() {
    return(m_time < rhs.m_time);
}

bool MusicTime::operator <=(const MusicTime &rhs) const throw() {
    return(m_time <= rhs.m_time);
}

bool MusicTime::operator >(const MusicTime &rhs) const throw() {
    return(m_time > rhs.m_time);
}

bool MusicTime::operator >=(const MusicTime &rhs) const throw() {
    return(m_time >= rhs.m_time);
}

/*unsigned */long long MusicTime::toMilliseconds() const throw() {
    return(m_time / 96);
}

unsigned long long MusicTime::toSamples(unsigned int freq) const throw() {
    return((m_time * freq) / 96000);
}

TCHAR *MusicTime::toStringAsMeasures(TCHAR *buf, unsigned int len, unsigned int bpm, unsigned int sigBpm, unsigned int sigBnt) const throw() {
    unsigned int samplesPerMeasure = (96000 * sigBpm * 60) / bpm;
    unsigned int samplesPerBeat = (96000 * 60) / bpm;
    unsigned int measure = m_time / samplesPerMeasure;
    unsigned int beat = (m_time - (measure * samplesPerMeasure)) / samplesPerBeat;
    wsprintf(buf, TEXT("%d:%d"), 1 + measure, 1 + beat);

    return(buf);
}
